package com.wnn.demo.model.system.controller;


import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Permission;
import com.wnn.demo.domain.Role;
import com.wnn.demo.model.system.service.RoleService;
import com.wnn.demo.pojo.Result;
import com.wnn.demo.pojo.ResultCode;
import com.wnn.demo.vo.MenuTreeVo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @RequestMapping(value = "/role",method = RequestMethod.GET)
    public Result findAll( @RequestParam(defaultValue = "1")Integer page,
                           @RequestParam(defaultValue = "10")Integer limit){
        PageInfo< Role > pageInfo = roleService.findAll(page, limit);
        return new Result(ResultCode.OK,pageInfo.getTotal(),pageInfo.getList());

    }

    @RequestMapping(value = "/role/{id}",method = RequestMethod.GET)
    public Result findRoleById(@PathVariable(value = "id")String id ){
        Role role = roleService.findRoleById(id);
        return new Result(ResultCode.OK,role);

    }


    @RequestMapping(value = "/role",method = RequestMethod.POST)
    public Result addRole(@RequestBody Map<String,Object> map){


        int i = roleService.addRole(map);
        if (i>0){
            return Result.ok();
        }
        return Result.failure();
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    @RequiresRoles(value={"admin","administrator"},logical= Logical.OR)
    @RequestMapping(value="/batchDeleteRoleByIds",method = RequestMethod.POST)
    public Result batchDeleteUserByIds(String ids) {
        System.out.println("roleIds"+ids);
        int i = roleService.batchDeleteRoleByIds(ids);
        if (i==0){
            return Result.failure();
        }
        return Result.ok();
    }


    /**
     * 根据Id删除角色
     * @param id
     * @return
     */
    @RequiresRoles(value={"admin","administrator"},logical= Logical.OR)
    @RequestMapping(value = "/role/{id}",method = RequestMethod.DELETE)
    public Result deleteRoleById(@PathVariable(value = "id")String id){


        int i = roleService.deleteRoleById(id);
        if (i>0){
            return Result.ok();
        }
        return Result.failure();
    }

    @RequestMapping(value = "/role/{id}",method = RequestMethod.PUT)
    public Result saveRole(@PathVariable(value = "id")String id,@RequestBody Map<String,Object> map){

        int i = roleService.saveRole(id,map);
        if (i>0){
            return Result.ok();
        }
        return Result.failure();
    }



    /**
     * 角色模块-分配角色权限界面 初始化权限树 查找当前角色拥有的权限
     * @param roleId 角色ID
     * @return
     */
    @RequestMapping(value = "/findRoleHavePerm",method = RequestMethod.GET)
    public Result findRoleHavePerm(String roleId){
        List< MenuTreeVo > result = roleService.findRoleHavePerm(roleId);
        return new Result(ResultCode.OK,result);
    }


    /**
     * 分配角色权限
     * @param roleId
     * @param permissionList
     * @return
     */
    @RequiresRoles(value={"admin","administrator"},logical= Logical.OR)
    @RequestMapping(value = "/assignRolePerm/{id}",method = RequestMethod.PUT)
    public Result  assignRolePerm(@PathVariable(value = "id")String roleId,@RequestBody  List<Permission> permissionList){
        int i = roleService. assignRolePerm(roleId,permissionList);
        return new Result(ResultCode.OK);
    }




}
